<template>
  <div class="app-container">
    <el-row>
      <el-col :span="22">
        <el-form ref="queryForm" :model="queryParams" :inline="true">
          <el-form-item label="姓名" prop="roleName">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      
      <el-col :span="2">
        <div>
          <el-badge :is-dot="true">
            <el-button @click="addFriedn" icon="el-icon-message"></el-button>
          </el-badge>
        </div>
      </el-col>
    </el-row>

    <ul>
      <li v-for="item in userList" :key="item.id">
        <div class="avatar"><img :src="item.avatar"/></div>
        <div class="content">
          <h2>{{ item.name }}</h2>
          <div style="margin-top: 20px">{{ item.tagName }}</div>
        </div>
        <div style="margin-right: 40%; margin-top: 30px">
          <h2>{{ item.headClass }}</h2>
        </div>
        <div class="timestamp">
          <el-button
            @click="send(item.id)"
            class="friend_button"
            type="primary"
            :disabled="!isSendAdd(item.id)"
            :plain="!isSendAdd(item.id)"
            >{{ isSendAdd(item.id) ? "添加好友" : "已申请" }}</el-button
          >
        </div>

      </li>
    </ul>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      
    </el-dialog>
  </div>
</template>
  
  <script>
// import { getToken } from '@/utils/auth'
import {
  getUser,
  delUser,
  addUser,
  exportUser,
  updateUser,
  changeUserStatus,
} from "@/api/system/user";
import { mayAdd, snedAddFriend } from "@/api/social/add";
export default {
  name: "User",
  data() {
    return {
      //学习方向集合
      studyList: [],
      exportLoading: false,
      // 查询参数
      queryParams: {
        name: undefined,
      },
      title: "申请列表",
      // 是否显示弹出层
      open: false,
      // 表单校验
      // 角色选项
      roleOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      userList: null,
      //当前用户发送添加请求的列表
      sendFriend: [],
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {
        status: 0,
        id: undefined,
      },
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    addFriedn(){
      this.open = true;
    },
    /**发送添加用户申请 */
    send(id) {
      snedAddFriend(id).then((response) => {
        if (response.code == 200) {
          this.getList();
        }
      });
    },
    /**判断当前用户是否发送过添加申请 */
    isSendAdd(id) {
      return !this.sendFriend.some((item) => item.id === id);
    },
    /**导出所有用户 */
    export() {
      console.log("asdasd");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      mayAdd(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.sendFriend = response.applications;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      const text = row.status === 0 ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.userName + '"用户吗？')
        .then(function () {
          return changeUserStatus(row.id, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === 0 ? 1 : 0;
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then((response) => {
        this.form = response.user;
        this.roleOptions = response.roles;
        this.form.roleIds = response.roleIds;
        this.open = true;
        this.title = "修改用户";
        this.form.password = response.password;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 导出所有用户为Excel */
    handleExport() {
      this.$modal
        .confirm("是否确认导出所有分类数据？")
        .then(() => {
          this.exportLoading = true;
          return exportUser();
        })
        .then((response) => {
          // this.$download.name(response.msg)
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        //后续需要修改，把取反去了
        if (!valid) {
          if (this.form.id !== undefined) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.message-icon{
  position: relative;
}
.icon-stay {
  font-size: 40px;
  border: 0px;
  margin-left: 80%;
}
.message-icon::before {
  content: "";
  border: 5px solid red; /*设置红色*/
  border-radius: 10px; /*设置圆角*/
  position: absolute;
  z-index: 1000;
  right: 0;
  margin-top: -13px;
  margin-right: -5px;
}
.friend_button {
  width: 100px;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  display: flex;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.avatar {
  margin-right: 30px;
}

.avatar img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}

.content {
  flex-grow: 1;
}

.content h2 {
  font-size: 18px;
  margin: 0;
}

.content p {
  font-size: 14px;
  color: #888;
  margin: 5px 0 0 0;
}

.timestamp {
  font-size: 12px;
  color: #bbb;
}
</style>